<template>
  <div class="app-container sales-performance">
    <el-row class="header-form card-filter-box" style="margin-top: 24px;">
      <el-form :inline="true" :model="queryForm" class="query-form">
        <el-form-item>
          <el-date-picker
            v-model="queryForm.dateTime"
            :unlink-panels="true"
            :clearable="false"
            :editable="false"
            type="monthrange"
            size="small"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="timestamp"
            format="yyyy/MM"
            @change="datePickerTime"
          />
        </el-form-item>
        <el-form-item class="query-button">
          <!-- <el-button class="search-btn-style" type="primary" size="small" plain @click="queryClick">查询</el-button> -->
          <el-button class="clear-btn-style" type="text" size="small" @click="queryEmpty">
            清空
            <img :src="clearIcon" class="clear-icon" alt="clear">
          </el-button>
        </el-form-item>
      </el-form>
    </el-row>
    <div class="table-list">
      <el-row>
        <Title title="财务流水汇总" style="margin: 13px 0 16px 0"/>
        <el-col :span="24" class="sales-detail">
          <el-col :xs="7" :sm="7" :md="7" :lg="7" :xl="7" class="sale-detail-box">
            <div class="sales-amount">
              <img :src="allAmount" alt="allAmount">
            </div>
            <div>
              <div class="sales-text">{{ (list.totalAccount / 100) | moneyFilter }}<span class="unit">元</span></div>
              <div class="sales-title">总到账</div>
            </div>
          </el-col>
          <el-col :xs="17" :sm="17" :md="17" :lg="17" :xl="17" class="sale-detail-box">
            <div class="sales-amount">
              <img :src="notWrittenAmount" alt="notWrittenAmount">
            </div>
            <div>
              <div class="sales-text">{{ (list.totalWriteOffAmount / 100) | moneyFilter }}<span class="unit">元</span></div>
              <div class="sales-title">未核销金额</div>
            </div>
          </el-col>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <Title :title="'销售订单业绩'" style="padding: 16px 0 24px 0"/>
        </el-col>
        <el-table
          v-loading="listLoading"
          v-if="list && list.items && list.items.length > 0"
          :data="list.items"
          :summary-method="getSummaries"
          :header-cell-style="{background: '#F5F5FB',color: '#141C3D'}"
          element-loading-text="加载中..."
          empty-text="暂无数据"
          show-summary
          border
          fit
          hightlight-current-row
          class="table-list-box"
        >
          <el-table-column align="center" prop="market" label="销售" width="176">
            <template slot-scope="scope">{{ scope.row.market }}</template>
          </el-table-column>
          <template v-if="list.columns">
            <el-table-column
              v-for="(item, index) in list.columns"
              :key="index"
              :prop="item.keyword"
              :label="item.label"
              align="center"
              width="176">
              <template slot-scope="scope">{{ scope.row[item.keyword] / 100 | moneyFilter }}</template>
            </el-table-column>
          </template>
          <!-- <el-table-column align="center" prop="personalBooks" label="个人核销" width="176">
            <template slot-scope="scope">{{ scope.row.personalBooks / 100 | moneyFilter }}</template>
          </el-table-column>
          <el-table-column align="center" prop="mediaDelivery" label="媒体投放" width="176">
            <template slot-scope="scope">{{ scope.row.mediaDelivery / 100 | moneyFilter }}</template>
          </el-table-column>
          <el-table-column align="center" prop="mediaPlacement" label="媒介投放" width="176">
            <template slot-scope="scope">{{ scope.row.mediaPlacement / 100 | moneyFilter }}</template>
          </el-table-column>
          <el-table-column align="center" prop="singlePlanning" label="单条策划" width="176">
            <template slot-scope="scope">{{ scope.row.singlePlanning / 100 | moneyFilter }}</template>
          </el-table-column>
          <el-table-column align="center" prop="cps" label="CPS" width="176">
            <template slot-scope="scope">{{ scope.row.cps / 100 | moneyFilter }}</template>
          </el-table-column> -->
          <el-table-column align="center" prop="totalCost" label="成本" width="176">
            <template slot-scope="scope">{{ scope.row.totalCost / 100 | moneyFilter }}</template>
          </el-table-column>
          <el-table-column align="center" prop="totalPerformance" label="业绩金额" width="176">
            <template slot-scope="scope">{{ scope.row.totalPerformance / 100 | moneyFilter }}</template>
          </el-table-column>
          <el-table-column align="center" prop="actualOrderAmount" label="订单金额" min-width="176">
            <template slot-scope="scope">{{ scope.row.actualOrderAmount / 100 | moneyFilter }}</template>
          </el-table-column>
        </el-table>
        <div v-else-if="!init" class="empty-container">
          <img :src="emptyIcon" class="empty-img" alt="">
          <div class="empty-title">暂无数据</div>
        </div>
      </el-row>
    </div>
  </div>
</template>
<script>
import Empty from '@/mixins/empty';
import { getList } from '@/api/salesPerformance';
import Title from '@/components/Title';
import { moneyFilter } from '@/filters/number';
import clearIcon from '@/assets/medium/clear.png';
import notWrittenAmount from '@/assets/notWrittenAmount.png';
import allAmount from '@/assets/allAmount.png';
import emptyIcon from '@/assets/empty-table-data.png';
export default {
  components: { Title },
  filters: { moneyFilter },
  mixins: [Empty],
  data() {
    return {
      emptyIcon,
      clearIcon,
      notWrittenAmount,
      allAmount,
      queryForm: {
        dateTime: ''
      },
      queryData: {
        createStart: '',
        createEnd: ''
      },
      list: {
        items: [],
        totalAccount: '',
        totalWriteOffAmount: ''
      },
      listLoading: true,
      currentMonth: '',
      createEnd: '',
      createStart: '',
      init: true
    };
  },
  created() {
    // 初始化为当前月
    this.currentMonth = new Date().setTime(new Date(new Date().getFullYear(), new Date().getMonth()));
    this.queryForm.dateTime = [this.currentMonth, this.currentMonth];
    const obj = {
      createStart: this.currentMonth / 1000,
      createEnd: this.currentMonth / 1000
    };
    this.createStart = obj.createStart;
    this.createEnd = obj.createEnd;
    this.queryData = obj;
    this.fetchData();
  },
  methods: {
    fetchData() {
      getList(this.queryData)
        .then(
          ({ data }) => {
            this.init = false;
            this.list = data;
            this.listLoading = false;
          },
          result => {
            this.init = false;
            // this.$message.error(result.message);
            this.listLoading = false;
          }
        )
        .catch(e => {
          console.error(e);
        });
    },
    // 设置合计
    getSummaries(params) {
      const { columns, data } = params;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = '合计';
          return;
        }
        const values = data.map(item => item[column.property]);
        sums[index] = (
          values.reduce((prev, curr) => {
            return prev + curr;
          }, 0) / 100
        ).toFixed(2);
        sums[index] = moneyFilter(sums[index]);
      });
      return sums;
    },
    datePickerTime(val) {
      if (!val) return false;
      this.createStart = Math.round(val[0] / 1000);
      this.createEnd = Math.round(val[1] / 1000);
      this.queryClick();
    },
    queryClick() {
      if (!this.queryForm.dateTime || JSON.stringify(this.queryForm) === '{}') {
        this.$message.warning('搜索条件不能为空');
      } else {
        this.queryData.createStart = this.createStart;
        this.queryData.createEnd = this.createEnd;
        this.fetchData();
      }
    }
  }
};
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
.sales-performance {
  .header-form {
    .query-form {
      display: flex;
      align-items: center;
      & ::v-deep {
        .el-form-item {
          margin-right: 16px;
        }
        .el-form-item__content {
          display: flex;
        }
        .el-date-editor {
          width: 224px !important;
        }
      }
    }
  }
  .table-list {
    min-height: 1000px;
    padding: 11px 24px 24px 24px;
    margin: 0px 20px 20px 20px;
    box-shadow:2px 4px 13px 0px rgba(221,225,234,1);
    border-radius:12px;
    background-color: #fff;
    .el-row {
      padding: 0;
    }
    .table-list-box {
      color: #999;
    }
  }
  .el-row {
    &:nth-of-type(2) {
      margin: 0;
    }
    &:last-child {
      margin-bottom: 0;
      padding: 0;
    }
    .sales-detail {
      margin-top: 24px;
      margin-bottom: 46px;
      .sale-detail-box {
        display: flex;
        justify-content: flex-start;
        align-items: center;
        .sales-amount {
          width: 48px;
          height: 48px;
          margin-right: 16px;
          img {
            width: 100%;
            height: 100%;
          }
        }
      }
      .sales-title {
        display: inline-block;
        font-size: 14px;
        color: #999;
      }
      .sales-text {
        font-size: 20px;
        color: #333;
        margin-bottom: 8px;
        font-weight: 500;
        .unit {
          display: inline-block;
          font-size: 14px;
          margin-left: 2px;
        }
      }
    }
    .el-table {
      border-radius: 10px;
      font-size: 14px;
    }
  }
}
.empty-container {
  text-align:center;
  margin-top:50px;
  .empty-img {
    width: 120px;
  }
  .empty-title {
    font-size: 15px;
    // margin-top: 20px;
    text-align: center;
  }
}
</style>

